﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.CatalogUI;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using System.Threading;

namespace ODM_Code.Forms
{
    public partial class CreateBasemapForm : Form
    {
        private string dbfPath = "C:\\magfire\\" + ConfigSettings.ReadSetting("analysisArea");
        private string inputPath = "C:\\magfire\\" + ConfigSettings.ReadSetting("analysisArea") + "\\input";
        private string copyimportPath = "C:\\magfire\\" + ConfigSettings.ReadSetting("analysisArea") + "\\copyimport";
        private ODM_Code.Code.CreateBasemapCode cbc;

        private IMap map;
        private IActiveView view;

        public CreateBasemapForm()
        {
            InitializeComponent();
        }

        public CreateBasemapForm(IMxDocument pMxDoc)
        {
            InitializeComponent();
            cbc = new Code.CreateBasemapCode(copyimportPath);
            map = pMxDoc.FocusMap;
            view = pMxDoc.ActiveView;
        }

        private void butOK_Click(object sender, EventArgs e)
        {
            if (this.CheckTextFields() == false)
            {
                MessageBox.Show("All text boxes must be filled.", "Missing Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.Driver();
            this.Close();
        }

        private void butLoadPathToProjectLayer_Click(object sender, EventArgs e)
        {
            string path = this.OpenFileHelper("maskLayer", "Load project mask layer", true);
            tbPathToProjectMask.Text = path;
        }

        private void butLoadTreatment_Click(object sender, EventArgs e)
        {
            string path = this.OpenFileHelper("treatLayer", "Load treatments shape file", true);
            tbPathToTreatment.Text = path;
        }

        private void butLoadAspect_Click(object sender, EventArgs e)
        {
            string path = this.OpenFileHelper("aspect", "Load aspect raster", false);
            tbPathToAspect.Text = path;
        }

        private void butLoadElevation_Click(object sender, EventArgs e)
        {
            string path = this.OpenFileHelper("elevation", "Load elevation raster", false);
            tbPathToElevation.Text = path;
        }

        private void butLoadSlope_Click(object sender, EventArgs e)
        {
            string path = this.OpenFileHelper("slope", "Load slope raster", false);
            tbPathToSlope.Text = path;
        }

        private void butLoadTreatgrid_Click(object sender, EventArgs e)
        {
            string path = this.OpenFileHelper("treatgrid", "Load treatgrid raster", false);
            tbPathToTreatgrid.Text = path;
        }

        private void Driver()
        {
            cbc.LoadAllTheLayers();

            cbc.ProcessData();

            //Thread worker = new Thread(new ThreadStart(cbc.ProcessData));
            //try
            //{
            //    worker.Start();
            //    worker.Join();
            //}
            //catch (ThreadStateException ex)
            //{
            //    System.Diagnostics.Trace.WriteLine(ex.Message);
            //}
            //catch (ThreadInterruptedException ex)
            //{
            //    System.Diagnostics.Trace.WriteLine(ex.Message);
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine(ex.Message);
            //}

        }
        
        /// <summary>
        /// Auto populates text boxes with 30m raster paths
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butLoad30m_Click(object sender, EventArgs e)
        {
            tbPathToTreatment.Text = "C:\\magfire\\masterlayers\\standc4r.shp";
            tbPathToAspect.Text = "C:\\magfire\\masterlayers\\aspect";
            tbPathToElevation.Text = "C:\\magfire\\masterlayers\\elevation";
            tbPathToSlope.Text = "C:\\magfire\\masterlayers\\slope";
            tbPathToTreatgrid.Text = "C:\\magfire\\masterlayers\\treatgrid";

            string path;

            path = tbPathToTreatment.Text;
            cbc.layerInfoDict["treatLayer"] = new Code.CreateBasemapCode.LayerData(path, "standc4r");
            path = tbPathToAspect.Text;
            cbc.layerInfoDict["aspect"] = new Code.CreateBasemapCode.LayerData(path, "aspect");
            path = tbPathToElevation.Text;
            cbc.layerInfoDict["elevation"] = new Code.CreateBasemapCode.LayerData(path, "elevation");
            path = tbPathToSlope.Text;
            cbc.layerInfoDict["slope"] = new Code.CreateBasemapCode.LayerData(path, "slope");
            path = tbPathToTreatgrid.Text;
            cbc.layerInfoDict["treatgrid"] = new Code.CreateBasemapCode.LayerData(path, "treatgrid");
        }

        /// <summary>
        /// Auto populates text boxes with 90m raster paths
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butLoad90m_Click(object sender, EventArgs e)
        {
            tbPathToTreatment.Text = "C:\\magfire\\masterlayers\\standc4r.shp";
            tbPathToAspect.Text = "C:\\magfire\\masterlayers\\asp90";
            tbPathToElevation.Text = "C:\\magfire\\masterlayers\\ele90";
            tbPathToSlope.Text = "C:\\magfire\\masterlayers\\slp90";
            tbPathToTreatgrid.Text = "C:\\magfire\\masterlayers\\treat90";

            string path;

            path = tbPathToTreatment.Text;
            cbc.layerInfoDict["treatLayer"] = new Code.CreateBasemapCode.LayerData(path, "standc4r");
            path = tbPathToAspect.Text;
            cbc.layerInfoDict["aspect"] = new Code.CreateBasemapCode.LayerData(path, "aspect");
            path = tbPathToElevation.Text;
            cbc.layerInfoDict["elevation"] = new Code.CreateBasemapCode.LayerData(path, "elevation");
            path = tbPathToSlope.Text;
            cbc.layerInfoDict["slope"] = new Code.CreateBasemapCode.LayerData(path, "slope");
            path = tbPathToTreatgrid.Text;
            cbc.layerInfoDict["treatgrid"] = new Code.CreateBasemapCode.LayerData(path, "treatgrid");
        }

        private string OpenFileHelper(string key, string message, bool isFeature)
        {
            string outPath = null;
            string outName = "";
            IGxDialog gxDialog = new GxDialogClass();
            gxDialog.AllowMultiSelect = false;

            IGxObjectFilter filter = null;
            if (isFeature)
            {
                filter = new GxFilterPolygonFeatureClasses();
            }
            else
            {
                filter = new GxFilterRasterDatasetsClass();
            }

            gxDialog.ObjectFilter = filter;
            gxDialog.Title = message;

            IEnumGxObject eGxObj;

            if (gxDialog.DoModalOpen(0, out eGxObj))
            {
                IGxObject gxObj = eGxObj.Next();
                outPath = gxObj.FullName;
                outName = gxObj.BaseName;
            }

            cbc.layerInfoDict[key] = new Code.CreateBasemapCode.LayerData(outPath, outName);

            return outPath;
        }

        private bool CheckTextFields()
        {
            bool good = true;
            TextBox[] tbs = {
                                tbPathToAspect, 
                                tbPathToElevation, 
                                tbPathToProjectMask, 
                                tbPathToSlope, 
                                tbPathToTreatment
                            };
            foreach (TextBox tb in tbs)
            {
                if (tb.Text.Equals(""))
                {
                    good = false;
                }
            }
            return good;
        }

        

        
    }
}
